package com.shopnight.controller;


import com.shopnight.result.ApiResult;
import com.shopnight.service.CartService;
import com.shopnight.service.UserService;
import com.shopnight.redisEntity.Cart;
import com.shopnight.redisEntity.CartItem;
import com.shopnight.redisEntity.CheckItemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/api/token")
@Api(tags = {"CartController"},description = "购物车页面所需接口 (所有接口均带token）")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 批量删除
     * @param orderIds
     * @return
     */
    @ApiOperation(value = "批量删除购物项",notes = "需要:JSON 商品id集合")
    @PostMapping("/deleteItems")
    public ApiResult deleteItems(@RequestBody Long[] orderIds){
        cartService.deleteItems(orderIds);
        return ApiResult.ok(null);
    }

    //改变购物项数量
    @GetMapping("/countItem")
    @ApiOperation(value = "修改购物项数量",notes = "需要:商品id，商品数量")
    public ApiResult countItem(@ApiParam("参数为：orderId") @RequestParam("orderId")  Long orderId,@ApiParam("参数为：num")  @RequestParam("num")  Integer num){
        cartService.changeItemCount(orderId,num);
        return ApiResult.ok(null);
    }

    /**
     * 勾选购物项选择状态
     * @return
     */
    @PostMapping("/checkItem")
    @ApiOperation(value = "批量修改购物项选中状态",notes = "需要:JSON 集合{orderId , isCheck}  (isCheck选中状态0(未选中) 1(选中))")
    public ApiResult checkItem(@RequestBody List<CheckItemVo> checkItemVos){
        cartService.checkItem(checkItemVos);
        return ApiResult.ok(null);
    }

    //查看购物车
    @GetMapping("/cart")
    @ApiOperation(value = "获取到整个购物车",notes = "需 orgId")
    public ApiResult cartListPage(@RequestParam Long orgId) throws ExecutionException, InterruptedException {
        Cart cart = cartService.getCart(orgId);
        return ApiResult.ok(cart);
    }

    /**
     * 添加商品到购物车
     */
    @GetMapping("/addToCart")
    @ApiOperation(value = "添加商品到购物车",notes = "需要:商品id,商品数量")
    public ApiResult<Object> addToCart( @ApiParam("参数为：orderId") @RequestParam("orderId") Long orderId,@ApiParam("参数为：num") @RequestParam("num") Integer num) throws ExecutionException, InterruptedException {
        CartItem cartItem = cartService.addToCart(orderId, num);
        return ApiResult.ok(cartItem);
    }

    /**
     * 获取单个购物项
     *
     */
    @GetMapping("/addToCartSuccess")
    @ApiOperation(value = "获取到单个购物项",notes = "需要:商品id")
    public ApiResult addToCartSuccessPage(@ApiParam("参数为：orderId")   @RequestParam("orderId") Long orderId){

        CartItem item = cartService.getCartItem(orderId);
        return ApiResult.ok(item);
    }



}
